package com.xin.springbootjurisdiction.service.impl;

import com.sun.corba.se.spi.ior.ObjectKey;
import com.xin.springbootjurisdiction.dataobject.AdminInfo;
import com.xin.springbootjurisdiction.repository.AdminInfoRepository;
import com.xin.springbootjurisdiction.service.AdminInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

@Service
@Slf4j
public class AdminInfoServiceImpl implements AdminInfoService {
    @Autowired
    private AdminInfoRepository adminInfoRepository;

    /**
     * 创建用户
     *
     * @param adminInfo
     * @return
     */
    @Override
    @Transactional
    public AdminInfo create(AdminInfo adminInfo) {
        if (ObjectUtils.isEmpty(adminInfo)) {
            return null;
        }
        AdminInfo info = adminInfoRepository.save(adminInfo);
        if (!ObjectUtils.isEmpty(info)) {
            return info;
        }
        return null;
    }

    /**
     * 删除用户
     * @param adminId
     * @return
     */
    @Override
    @Transactional
    public boolean delete(String adminId) {
        AdminInfo adminInfo = findOne(adminId);
        if (ObjectUtils.isEmpty(adminInfo)){
               return false;
        }
        adminInfoRepository.delete(adminId);
        return true;
    }

    /**
     * 查找用户
     * @param adminId
     * @return
     */
    @Override
    public AdminInfo findOne(String adminId) {
        if (StringUtils.isEmpty(adminId)){
              return null;
        }
        AdminInfo adminInfo = adminInfoRepository.findOne(adminId);
        return adminInfo;
    }

    /**
     * 更新用户
     * @param adminInfo
     * @return
     */
    @Override
    @Transactional
    public AdminInfo update(AdminInfo adminInfo) {
        if (ObjectUtils.isEmpty(adminInfo)){
              return null;
        }
        AdminInfo info = findOne(adminInfo.getAdminId());
        if (ObjectUtils.isEmpty(info)){
              return null;
        }
        BeanUtils.copyProperties(info,adminInfo);
        AdminInfo save = adminInfoRepository.save(info);
        if (!ObjectUtils.isEmpty(save)){
             return save;
        }
        return null;
    }
}
